package com.shyfu.controllers;

import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Svitlana on 03-Apr-14.
 */
@Controller
@RequestMapping("login/")
public class LoginController {

    @Secured(value = "ROLE_ADMIN")
    @RequestMapping("success")
    public ModelAndView getSuccess(HttpServletRequest request) {
        request.getUserPrincipal().getName();
        String hasAdmin = (String) request.getSession().getAttribute("hasAdmin");
        hasAdmin=request.getUserPrincipal().getName();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("success");
        modelAndView.addObject("admin", hasAdmin);
        return modelAndView;
    }

    @RequestMapping("error")
    public ModelAndView getError(HttpServletRequest request) {
        String hasAdmin = (String) request.getSession().getAttribute("hasAdmin");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        modelAndView.addObject("admin", hasAdmin);
        return modelAndView;
    }

    @RequestMapping("login")
    public ModelAndView getLogin(HttpServletRequest request) {
        String hasAdmin = (String) request.getSession().getAttribute("hasAdmin");
        hasAdmin=request.getUserPrincipal().getName();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        modelAndView.addObject("admin", hasAdmin);
        return modelAndView;
    }

    @RequestMapping("logout")
    public ModelAndView getLogout(HttpServletRequest request) {
        String hasAdmin = (String) request.getSession().getAttribute("hasAdmin");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("logout");
        modelAndView.addObject("admin", hasAdmin);
        return modelAndView;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String go(HttpServletRequest request) {
        return "redirect:/success";
    }

}
